﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using Gacn.ControleAlunos.Library.Entities;

namespace Gacn.ControleAlunos.Library.DataAccess
{
    internal static class TurmaDataAccess
    {

        #region Get Methods
        public static DataTable GetAllTurmaEntity()
        {
            SqlConnection connection = null;
            SqlCommand command = null;
            DataTable dataTable = null;


            connection = Util.GetConnection();
            connection.Open();

            command = connection.CreateCommand();
            command.CommandText = "SELECT [idTurma],[CodTurma] FROM [tbTurma] WHERE [flgAtivo] = 'true' ORDER BY CodTurma";
            command.CommandType = CommandType.Text;

            //command.Parameters.Add("SUCESSO", SqlDbType.Bit).Direction = ParameterDirection.Output;
            //command.Parameters.Add("ProcComment", SqlDbType.VarChar, 1000).Direction = ParameterDirection.Output;

            dataTable = new DataTable();
            dataTable.Locale = CultureInfo.InvariantCulture;
            new SqlDataAdapter(command).Fill(dataTable);

            //if (!(bool)command.Parameters["SUCESSO"].Value)
            //{
            //    throw new Exception(string.Concat("Ocorreu um erro ao executar a procedure. \r\n", command.Parameters["ProcComment"].Value.ToString()));
            //}

            return dataTable;
        }
        #endregion

        #region Insert Methods
        /// <summary>
        /// Método Não Testado
        /// </summary>
        /// <param name="turma"></param>
        /// <returns></returns>
        public static DataTable NewTurmaDetalhadaEntity(TurmaDetalhadaEntity turma)
        {

            SqlConnection connection = null;
            SqlCommand command = null;
            DataTable dataTable = null;


            connection = Util.GetConnection();
            connection.Open();

            command = connection.CreateCommand();
            command.CommandText = "NomedaUSP";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add("flgAtivo", SqlDbType.VarChar).Value = turma.Ativo;
            command.Parameters.Add("idEscola", SqlDbType.VarChar).Value = turma.CodTurma;
            command.Parameters.Add("idCurso", SqlDbType.VarChar).Value = turma.CursoID;
            command.Parameters.Add("idEscola", SqlDbType.VarChar).Value = turma.EscolaID;
            command.Parameters.Add("dtInsert", SqlDbType.VarChar).Value = turma.Insercao;
            command.Parameters.Add("idInsertUser", SqlDbType.VarChar).Value = turma.InsertUserID;
            command.Parameters.Add("obs", SqlDbType.VarChar).Value = turma.Observacao;
            command.Parameters.Add("nrSala", SqlDbType.VarChar).Value = turma.Sala;

            command.Parameters.Add("ID", SqlDbType.Int).Direction = ParameterDirection.Output;
            command.Parameters.Add("SUCESSO", SqlDbType.Bit).Direction = ParameterDirection.Output;
            command.Parameters.Add("ProcComment", SqlDbType.VarChar, 1000).Direction = ParameterDirection.Output;

            dataTable = new DataTable();
            dataTable.Locale = CultureInfo.InvariantCulture;
            new SqlDataAdapter(command).Fill(dataTable);

            if (!(bool)command.Parameters["SUCESSO"].Value)
            {
                throw new Exception(string.Concat("Ocorreu um erro ao executar a procedure. \r\n", command.Parameters["ProcComment"].Value.ToString()));
            }

            return dataTable;
        }
        #endregion

    }
}
